angular.module('App').factory('Action', ['$http', 'Config', '$rootScope', '$httpParamSerializerJQLike','$cookies',
    function ($http, Config, $rootScope, $httpParamSerializerJQLike,$cookies) {
        var obj = {},
            loadi;
        obj.ajax = function (arg) {  //数据请求
            let { url, type = 'post', cache = !1, params, fn } = arg;
                   var xmlArg={
                       url: url,
                       method: type,
                       cache: cache,
                       headers: {
                           "Content-Type": "application/x-www-form-urlencoded",
                           Connection: 'Keep-Alive'
                            // 'Access-Control-Allow-Origin': '*'
                       },   // post  使用{data: params} 必写
                   };
                   cache ? '' : loadi = layer.load(1);

                   type === 'get' ? xmlArg['params']=params : xmlArg['data']=$httpParamSerializerJQLike(params);  //post  使用{data: params} 必写$httpParamSerializerJQLike
                   var http=$http(xmlArg);
                   http.then(function (data) {
                    //    angular.element('.removeWrap').remove();
                      layer.close(loadi);
                       var resTxt=data.data || {};
                       fn&&fn(resTxt);
                   });

                   return http;
        };

        obj.turnTime = function (str) {  //处理时间的兼容性 转时间戳
            return new Date(str).getTime() / 1000 ? new Date(str).getTime() / 1000 : function () {
                var arr = str.replace(/^\s+|\s+$/g, '').split(/\s+/),
                    time = arr[1].split(':');
                var date = new Date(arr[0]);

                date.setHours(time[0]);
                date.setMinutes(time[1]);

                return date.getTime() / 1000;

            }();
        };

        obj.msg=function(str,bool,fn){
            var arg = bool ? 6: 5; //6是成功  5是悲伤  //默认是悲伤
            layer.msg(str,{icon:arg},fn);
        };

        obj.cookie = function (arg) {
            var cookie = $cookies.getAll();
            return arg !== undefined && arg !== null ? cookie[arg] : cookie;
        };

        obj.toUrl=function(arg){
            if (typeof arg !== 'object'){
                return '';
            }
            var opt=JSON.stringify(arg);
                opt=encodeURI(opt);
                return opt;
        };

        obj.fromUrl=function(arg){
            if(typeof arg !== 'string'){
                return '';
            }
            var opt=decodeURI(arg);
                opt=JSON.parse(opt);
                return opt;
        };

        obj.wrapper = function () {
            var $ele = angular.element('.main-wrapper');
            if ($ele.length) {
                var hei = window.innerHeight - parseInt($ele.offset().top) - 30 + 'px';
                $ele.css('min-height',hei);
            }
        };

        var rsa = new RSAKey();
        rsa.setPublic(Config['rsa_a'], Config['rsa_b']);
        //https://github.com/cqingt/RSA_JS_PHP

        obj.rsa = function (arg) {
            return arg !== undefined ? rsa.encrypt(arg) : '';
        };

        window.obj = obj;  //测试时存在

        return obj;
    }]);